/*
 *  JVending - JSP Tag Library
 *  Copyright (C) 2004  Shane Isbell
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.jvending.provisioning.taglib;

import javax.provisioning.Deliverable;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.util.logging.Logger;

/**
 * @author Shane Isbell
 * @since 1.3.9a
 */

public class IconTag extends TagSupport {

    private static Logger logger = Logger.getLogger("IconTag");

    private Deliverable icon;

    private String width;

    private String height;

    public String getWidth() {
        return width;
    }

    public void setWidth(String width) {
        this.width = width;
    }

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public int doStartTag() throws JspTagException {
        icon = (Deliverable)
                pageContext.getAttribute("icon",
                        pageContext.PAGE_SCOPE);

        return SKIP_BODY;
    }

    public int doEndTag() throws JspTagException {
        if (icon == null) return EVAL_PAGE;

        try {
            logger.finest("Icon Tag: URL = " + icon.getURL());
            StringBuffer dimension = new StringBuffer();
            if (width != null) dimension.append("width = '" + width + "'");
            if (height != null) dimension.append(" height = '" + height + "'");
            pageContext.getOut().println("<img " + dimension + " src = '" + icon.getURL() + "'/>");
        } catch (IOException e) {
            e.printStackTrace();
            throw new JspTagException("Could not End Tag");
        }
        return EVAL_PAGE;
    }
}